package com.elysium.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elysium.constants.Constants;
import com.elysium.constants.GlobalExceptionCode;
import com.elysium.feign.AppLoginFeignClient;
import com.elysium.pojo.domain.Customer;
import com.elysium.mapper.CustomerMapper;
import com.elysium.pojo.domain.CustomerSummary;
import com.elysium.pojo.domain.CustomerWallet;
import com.elysium.pojo.param.RegisterSaveLoginParam;
import com.elysium.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elysium.service.ICustomerSummaryService;
import com.elysium.service.ICustomerWalletService;
import com.elysium.template.AppWeChatTemplate;
import com.elysium.utils.AssertUtil;
import com.elysium.utils.BitStatesUtil;
import com.elysium.utils.NameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author ElysiumNetworks
 * @since 2024-07-31
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private AppWeChatTemplate appWeChatTemplate;

    @Autowired
    private ICustomerSummaryService customerSummaryService;

    @Autowired
    private ICustomerWalletService customerWalletService;

    @Autowired
    private AppLoginFeignClient appLoginFeignClient;

    @Override
    public void register(String openIdCode) {
        // 1.获取openId，校验用户是否已注册
        String openId = appWeChatTemplate.getOpenIdByCode(openIdCode);

        Customer customer = super.getOne(new QueryWrapper<Customer>().eq("open_id", openId));

        AssertUtil.isNull(customer, GlobalExceptionCode.PARAM_PHONE_EXIST);

        // 3.业务实现
        // 3.1.保存Customer信息

        customer = new Customer();
        saveCustomer(customer, openId);

        Long id = customer.getId();
        // 3.2.初始化从表：钱包、配置、统计
        String password = "123456";
        String hashpw = BCrypt.hashpw(password, BCrypt.gensalt());
        initCustomerAssociation(id, hashpw);

        // 3.3.调用feign接口保存Login
        saveLogin(id, openId, hashpw, Constants.Login.TYPE_CUSTOMER);
    }

    /**
     * 保存登录信息
     * @param openId
     */
    private void saveLogin(Long id, String openId, String password, Integer type) {
        String phone = "19713258175";
        RegisterSaveLoginParam registerSaveLoginParam = new RegisterSaveLoginParam();
        registerSaveLoginParam.setUsername(phone);
        registerSaveLoginParam.setPassword(password);
        registerSaveLoginParam.setType(type);
        registerSaveLoginParam.setEnabled(true);
        registerSaveLoginParam.setAvatar(Constants.DEFAULT_AVATAR);
        registerSaveLoginParam.setAdmin(false);
        String name = NameUtil.getName();
        registerSaveLoginParam.setNickName(name);
        registerSaveLoginParam.setOpenId(openId);
        registerSaveLoginParam.setName(name);
        registerSaveLoginParam.setPhone(phone);
        registerSaveLoginParam.setId(id);

        appLoginFeignClient.save(registerSaveLoginParam);
    }

    /**
     * 保存乘客关联表
     * @param id
     * @param password
     */
    private void initCustomerAssociation(Long id, String password) {
        CustomerSummary customerSummary = new CustomerSummary();
        customerSummary.setId(id);
        customerSummary.setTotalOrders(0);
        customerSummary.setTotalFine(new BigDecimal("0"));
        customerSummary.setVoucherNumbers(0);
        customerSummary.setCancelOrders(0);
        customerSummaryService.save(customerSummary);

        CustomerWallet customerWallet = new CustomerWallet();
        customerWallet.setId(id);
        customerWallet.setAmount(new BigDecimal("0"));
        customerWallet.setPassword(password);
        customerWallet.setDeleted(false);
        customerWallet.setVersion(0);
        customerWallet.setCreateTime(new Date());
        customerWalletService.save(customerWallet);
    }

    /**
     * 保存乘客
     * @param customer
     * @param openId
     */
    private void saveCustomer(Customer customer, String openId) {
        customer.setSex(Constants.SEX_MAN);
        customer.setPhone("19713258175");
        customer.setCreateTime(new Date());
        customer.setVersion(0);
        customer.setDeleted(false);
        customer.setOpenId(openId);
        customer.setBitState(BitStatesUtil.OP_PHONE);
        customer.setName(NameUtil.getName());
        customer.setLevel(Constants.Level.LEVEL_BRONZE);
        super.save(customer);

    }
}
